using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using DapperExtensions;
using ExaminationSys.DataAccess.Entity;
using SQLinq;
using Dapper;
using SQLinq.Dapper;

using DapperExtensions;

namespace ExaminationSys.DataAccess.Repository
{
    public class NewsTypeRepository : Repository<NewsType>
    {
        public NewsTypeRepository(SqlConnection context)
            : base(context)
        {
            db = context;
        }

        private SqlConnection db;

        public override NewsType GetById(dynamic id)
        {
            return base.ObjectSet.Get<NewsType>(db, id, null, null);
        }
    }

    public class NewsTypeService
    {
        private NewsTypeService()
        {
        }

        public NewsType GetNewsTypeByName(string name)
        {
            using (var db = DataContent.DBServer())
            {
                var result = db.Query(from p in new SQLinq<NewsType>()
                                      where p.Name == name 
                                      select p).FirstOrDefault();
                return result;
            }
        }

        public List<NewsType> GetAll()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.NewsType.GetList();
                return result.ToList();
            }
        }

        public List<NewsType> GetPublishList()
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var pg = new PredicateGroup {Operator = GroupOperator.And, Predicates = new List<IPredicate>()};
                pg.Predicates.Add(Predicates.Field<NewsType>(f => f.Status, Operator.Eq, 1));
                var result = work.NewsType.GetList(pg);
                return result.ToList();
            }
        }

        public bool CheckNameExists(string name, Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
                pg.Predicates.Add(Predicates.Field<NewsType>(f => f.Name, Operator.Eq, name));
                pg.Predicates.Add(Predicates.Field<NewsType>(f => f.ID, Operator.Eq, id, true));
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.NewsType.GetList(pg).FirstOrDefault();
                return result != null;
            }
        }
        public NewsType Get(Guid id)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                var result = work.NewsType.GetById(id);
                return result;
            }
        }
        public bool Update(NewsType entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.NewsType.Update(entity);
            }
        }
        public dynamic Insert(NewsType entity)
        {
            using (var db = DataContent.DBServer())
            {
                IUnitOfWork work = new UnitOfWork(db);
                return work.NewsType.Insert(entity, null);
            }
        }
    }
}